package edu.nctu.csie.jichang.dp.structural;

import java.util.HashMap;
import java.util.Map;

public class FlyWeight {
	public static void main(String[] args) {
		FPool tPool = new FPool();
		String tA1 = new String("A");
		String tA2 = new String("B");
		String tA3 = new String("A");
		FClass tF1 = tPool.getFlyWeightClass(tA1);
		FClass tF2 = tPool.getFlyWeightClass(tA2);
		FClass tF3 = tPool.getFlyWeightClass(tA3);
		System.out.println((tA1 == tA2) +" " + (tA1 == tA3));
		System.out.println((tF1 == tF2) +" " + (tF1 == tF3));
	}
}
class FClass{
	String msg;
	FClass(String msg){this.msg = msg;}
}
class FPool{
	Map<String,FClass> pool = new HashMap<String, FClass>();
	public FClass getFlyWeightClass(String msg) {
		 FClass out = pool.get(msg); 
		 if(out == null) {
			 out = new FClass(msg);
			 pool.put(msg, out); 
		 }
		 return out; 
	 }
}